package com.herui.ota.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.herui.ota.config.ControllerNames;
import com.herui.ota.entity.Device;
import com.herui.ota.entity.DeviceOta;
import com.herui.ota.pojo.form.*;
import com.herui.ota.service.DeviceBluNumbService;
import com.herui.ota.service.DeviceOtaService;
import com.herui.ota.service.DeviceService;
import com.herui.ota.service.iml.BaseService;
import com.mlnx.common.annotations.NeedLogin;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.enums.BusinessType;
import com.mlnx.common.form.PageForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.shan.spring.base.CurdBaseController;
import org.shan.spring.base.UrdBaseController;
import org.shan.spring.base.annotations.SysLog;
import org.shan.spring.mybatis.service.ICurdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;

@Api(value = ControllerNames.DeviceName + "接口", tags = {ControllerNames.DeviceName + "接口"})
@SysLog(title = ControllerNames.DeviceName)
@RestController
@RequestMapping("/device")
public class DeviceController extends UrdBaseController<Device,
        Device, DeviceFilterForm> {
    @Autowired
    private DeviceService service;

    @Autowired
    private DeviceBluNumbService deviceBluNumbService;

    @Override
    public ICurdService getService() {
        return service;
    }

    @Override
    public Device newInstance(Integer id) {
        Device soft = new Device();
        soft.setId(id);
        return soft;
    }

    @ApiOperation(
            value = "分页获取",
            notes = ""
    )
    @PostMapping({"/page"})
    public ResponseData<IPage> listPage(@Valid PageForm pageForm, DeviceFilterForm ft) throws IllegalAccessException {
        return this.result(this.getService().page(pageForm, ft));
    }

    @ApiOperation("导出excel列表")
    @GetMapping("/excel")
    public void getMqttParamById(PageForm pageForm, DeviceFilterForm filter, HttpServletResponse response) throws IllegalAccessException {
        BaseService baseService = (BaseService) service;
        baseService.pageExcel( pageForm, filter,response);
    }

    @NeedLogin
    @ApiOperation(
            value = "新增",
            notes = ""
    )
    @PostMapping({""})
    public Response add(HttpServletRequest request, @Valid DeviceForm form) throws IllegalAccessException,
            InstantiationException {
        service.saveDevice(request, form);
        return this.result();
    }

    @NeedLogin
    @ApiOperation(
            value = "获取设备总数",
            notes = ""
    )
    @GetMapping({"/count"})
    public ResponseData<Integer> count() {
        return this.result(service.allDeviceCount());
    }

    @NeedLogin
    @ApiOperation(
            value = "新建一个蓝牙编号",
            notes = ""
    )
    @GetMapping({"/newBlueName"})
    public ResponseData<Integer> newBlueName(String device_uuid) {
        return this.result(deviceBluNumbService.newBlueName());
    }

    @NeedLogin
    @ApiOperation(
            value = "重置树状图",
            notes = ""
    )
    @PutMapping({"/resetTree"})
    public Response resetTree() {
        service.resetTree();
        return this.result();
    }

    @NeedLogin
    @ApiOperation(
            value = "设备激活",
            notes = ""
    )
    @PutMapping({"/activeDevice"})
    public ResponseData<Date> activeDevice(String bluName){
        Date date = service.activeDevice(bluName);
        return this.result(date);
    }

    @NeedLogin
    @ApiOperation(
            value = "保存设备升级记录",
            notes = ""
    )
    @PostMapping({"/upgradeDevice"})
    public Response upgradeDevice(String bluName, boolean sucess){
        service.upgradeDevice(bluName, sucess);
        return this.result();
    }

}
